package test;

import common.Utils;
import org.apache.hc.core5.util.Asserts;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;

public class DetailArticle extends Utils {
    public static String url = "http://127.0.0.1:58080/index.html";
    public static ForumListPage forumListPage;
    public static WebDriverWait wait;
    public DetailArticle() {
        super(url);
         forumListPage = new ForumListPage();
         wait = new WebDriverWait(driver,Duration.ofSeconds(10));
    }

    /**
     * 检查点赞是否有问题
     */
    public void checkThumb () throws InterruptedException {
        Long preThumbs = Long.valueOf(driver.findElement(By.cssSelector("#details_article_likeCount")).getText());
        // 点击点赞
        driver.findElement(By.cssSelector("#details_btn_like_count")).click();
        Thread.sleep(200);
        Long actualThumbs = Long.valueOf(driver.findElement(By.cssSelector("#details_article_likeCount")).getText());
        Asserts.check(preThumbs + 1 == actualThumbs,"执行错误");
//        driver.findElement(By.cssSelector(""));
    }

    /**
     * 回复消息成功
     */
    public void replyMessageSuccess() throws InterruptedException {
        driver.get("http://127.0.0.1:58080/index.html");
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        WebElement codeMirror = driver.findElement(By.cssSelector("#article_details_reply .CodeMirror-scroll"));
        // 滚动到底部
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d) {
                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                        "return window.pageYOffset + window.innerHeight;");
                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                        "return document.body.scrollHeight;");
                // 使用 doubleValue() 进行比较
                return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
            }
        });
        codeMirror.click();
        Actions actions = new Actions(driver);
        actions.sendKeys("太强了兄弟").build().perform();
        Thread.sleep(1000);
        actions.click(driver.findElement(By.cssSelector("#details_btn_article_reply"))).perform();

    }

    /**
     * 回复消息失败
     */
    public void replyMessageFail() throws InterruptedException {
        WebElement codeMirror = driver.findElement(By.cssSelector("#article_details_reply .CodeMirror-scroll"));
        // 滚动到底部
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
        // 使用 WebDriverWait 等待直到页面滚动到底部
        wait.until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver d) {
                Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                        "return window.pageYOffset + window.innerHeight;");
                Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                        "return document.body.scrollHeight;");
                // 使用 doubleValue() 进行比较
                return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
            }
        });
        driver.findElement(By.cssSelector("#details_btn_article_reply")).click();
        // 捕获错误
        List<WebElement> elements = driver.findElements(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
        String text = elements.get(elements.size() - 1).getText();
        String[] texts = text.split("\n");
        assert texts[texts.length - 1].equals("请输入回复内容"); // 输出提示

    }


    /**
     * 如果是作者删除帖子失败
     * 选择取消
     */
    public void deleteArticleFail() throws InterruptedException {
        List<WebElement> elements = driver.findElements(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a > span"));
        if (!elements.isEmpty()) {
            elements.get(0).click();
            Thread.sleep(500);
            driver.findElement(By.cssSelector("#details_delete_modal > div > div > div.modal-footer > div > div > div:nth-child(1) > a")).click();
        } else {
            System.out.println("不是发帖者");
        }
    }


    /**
     * 如果是作者删除帖子成功
     */
    public void deleteArticleSuccess() throws InterruptedException {
        List<WebElement> elements = driver.findElements(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a > span"));
        if (!elements.isEmpty()) {
            elements.get(0).click();
            Thread.sleep(500);
            driver.findElement(By.cssSelector("#details_artile_delete")).click();
//            Alert alert = driver.switchTo().alert();
//            alert.accept();
        } else {
            System.out.println("不是发帖者");
        }
    }
    /**
     * 如果是作者可以编辑帖子
     * 编辑成功
     */
    public void modifyArticleSuccess () throws InterruptedException {
        driver.get("http://127.0.0.1:58080/index.html");
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        List<WebElement> elements = driver.findElements(By.cssSelector("#details_artile_edit"));
        if (!elements.isEmpty()) {
            elements.get(0).click();
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div.row.align-items-center > div.col-2 > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div:nth-child(3) > div.col-2 > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div:nth-child(4) > div.col-2 > h2"));
            driver.findElement(By.cssSelector("#edit_article_title")).sendKeys("面试经验1");
            WebElement codeMirrorElement = driver.findElement(By.cssSelector("#edit_article_content_area > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll"));
            codeMirrorElement.click();
            Actions actions = new Actions(driver);
            actions.doubleClick(codeMirrorElement)  // 点击元素以聚焦
                    .keyDown(Keys.CONTROL)   // 按下 Ctrl 键
                    .sendKeys("a")
                    .sendKeys(Keys.DELETE)  // 删除内容
                    .sendKeys("新的文字内容")  // 输入新内容
                    .perform();  // 执行操作
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
            // 使用 WebDriverWait 等待直到页面滚动到底部
            wait.until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                            "return window.pageYOffset + window.innerHeight;");
                    Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                            "return document.body.scrollHeight;");
                    // 使用 doubleValue() 进行比较
                    return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
                }
            });
            driver.findElement(By.cssSelector("#edit_article_submit")).click();
        } else {
            System.out.println("不是发帖者");
        }
    }

    /**
     * 编辑内容为空
     */
    public void modifyArticleFail () throws InterruptedException {
        driver.get("http://127.0.0.1:58080/index.html");
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        List<WebElement> elements = driver.findElements(By.cssSelector("#details_artile_edit"));
        if (!elements.isEmpty()) {
            elements.get(0).click();
            // 判断页面是否有问题
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div.row.align-items-center > div.col-2 > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div:nth-child(3) > div.col-2 > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div:nth-child(4) > div.col-2 > h2"));
            // 修改标题
            driver.findElement(By.cssSelector("#edit_article_title")).sendKeys("面经噢");
            // 编辑内容区
            WebElement codeMirrorElement = driver.findElement(By.cssSelector("#edit_article_content_area > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll"));
            codeMirrorElement.click();
            Actions actions = new Actions(driver);
            actions.doubleClick(codeMirrorElement)  // 点击元素以聚焦
                    .keyDown(Keys.CONTROL)   // 按下 Ctrl 键
                    .sendKeys("a")
                    .sendKeys(Keys.DELETE)  // 删除内容
                    .perform();  // 执行操作
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
            // 使用 WebDriverWait 等待直到页面滚动到底部
            wait.until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                            "return window.pageYOffset + window.innerHeight;");
                    Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                            "return document.body.scrollHeight;");
                    // 使用 doubleValue() 进行比较
                    return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
                }
            });
            driver.findElement(By.cssSelector("#edit_article_submit")).click();
            // 捕获错误
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
            String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
            String[] texts = text.split("\n");
            assert texts[texts.length - 1].equals("请输入帖子内容"); // 用户名过长导致警告
        } else {
            System.out.println("不是发帖者");
        }
    }

    /**
     * 编辑标题为空
     */
    public void modifyArticleFail1 () throws InterruptedException {
        driver.get("http://127.0.0.1:58080/index.html");
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        List<WebElement> elements = driver.findElements(By.cssSelector("#details_artile_edit"));
        if (!elements.isEmpty()) {
            elements.get(0).click();
            // 判断页面是否有问题
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div.row.align-items-center > div.col-2 > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div:nth-child(3) > div.col-2 > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div:nth-child(4) > div.col-2 > h2"));
            // 修改标题
            driver.findElement(By.cssSelector("#edit_article_title")).clear();
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
            // 使用 WebDriverWait 等待直到页面滚动到底部
            wait.until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                            "return window.pageYOffset + window.innerHeight;");
                    Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                            "return document.body.scrollHeight;");
                    // 使用 doubleValue() 进行比较
                    return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
                }
            });
            driver.findElement(By.cssSelector("#edit_article_submit")).click();
            // 捕获错误
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
            String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
            String[] texts = text.split("\n");
            assert texts[texts.length - 1].equals("请输入帖子标题"); // 用户名过长导致警告
        } else {
            System.out.println("不是发帖者");
        }
    }


    /**
     * 编辑内容为空, 标题为空
     */
    public void modifyArticleFail2 () throws InterruptedException {
        driver.get("http://127.0.0.1:58080/index.html");
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        List<WebElement> elements = driver.findElements(By.cssSelector("#details_artile_edit"));
        if (!elements.isEmpty()) {
            elements.get(0).click();
            // 判断页面是否有问题
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div.row.align-items-center > div.col-2 > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div:nth-child(3) > div.col-2 > h2"));
            driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div > div.card-body > div:nth-child(4) > div.col-2 > h2"));
            // 修改标题
            driver.findElement(By.cssSelector("#edit_article_title")).clear();
            // 编辑内容区
            WebElement codeMirrorElement = driver.findElement(By.cssSelector("#edit_article_content_area > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll"));
            codeMirrorElement.click();
            Actions actions = new Actions(driver);
            actions.doubleClick(codeMirrorElement)  // 点击元素以聚焦
                    .keyDown(Keys.CONTROL)   // 按下 Ctrl 键
                    .sendKeys("a")
                    .sendKeys(Keys.DELETE)  // 删除内容
                    .perform();  // 执行操作
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
            // 使用 WebDriverWait 等待直到页面滚动到底部
            wait.until(new ExpectedCondition<Boolean>() {
                @Override
                public Boolean apply(WebDriver d) {
                    Number scrollPosition = (Number) ((JavascriptExecutor) d).executeScript(
                            "return window.pageYOffset + window.innerHeight;");
                    Number scrollHeight = (Number) ((JavascriptExecutor) d).executeScript(
                            "return document.body.scrollHeight;");
                    // 使用 doubleValue() 进行比较
                    return scrollPosition.doubleValue() >= scrollHeight.doubleValue();
                }
            });
            driver.findElement(By.cssSelector("#edit_article_submit")).click();
            // 捕获错误
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
            String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
            String[] texts = text.split("\n");
            assert texts[texts.length - 1].equals("请输入帖子标题"); // 用户名过长导致警告
        } else {
            System.out.println("不是发帖者");
        }
    }

    /**
     * 检查是否可以访问发帖人的主页
     */
    public void visitUserHomepage () throws InterruptedException {
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        driver.findElement(By.cssSelector("#article_details_author_name")).click();
        // 检查界面元素
        driver.findElement(By.cssSelector("#profile_nickname"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.col-lg-4 > div > div > div > div > h2"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col > div > div:nth-child(3)"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col > div > div:nth-child(1)"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col > div > div:nth-child(2)"));
        // 检查是否可以访问发帖人的第一篇帖子
        driver.findElement(By.cssSelector("#profile_article_body > li > div.card.timeline-event-card > div > div > div > div > div.text-truncate > a > strong")).click();
        // 检查该页面的元素
        driver.findElement(By.cssSelector("#details_article_createTime"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2) > svg"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(2) > h3"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(4) > svg"));
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3) > svg"));

    }

    /**
     * 私信发送成功
     */
    public void sendMessageSuccess() throws InterruptedException {
        // 进入帖子详情
        driver.get("http://127.0.0.1:58080/index.html");
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(3) > div > div.col > div.text-truncate > a > strong")).click();
        // 查找是否有发私信键
        List<WebElement> elements = driver.findElements(By.cssSelector("#btn_details_send_message"));
        if (!elements.isEmpty()) {
            WebElement webElement = elements.get(0);
            // 确保私信键是可交互的的
            if (webElement.isDisplayed() && webElement.isEnabled()) {
                Thread.sleep(200);
                webElement.click();
                Thread.sleep(500);
                // 文本框
                WebElement element = driver.findElement(By.cssSelector("#index_message_receive_content"));
                element.sendKeys("okok");
                // 点击发送
                driver.findElement(By.cssSelector("#btn_index_send_message")).click();
                // 捕获错误
                Thread.sleep(500);
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
                String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
                String[] texts = text.split("\n");
                assert texts[texts.length - 1].equals("发送信息成功"); // 发送成功
            } else {
                System.out.println("不是发帖者");
            }
        } else {
            System.out.println("不是发帖者");
        }
    }

    /**
     * 私信发送失败, 消息内容为空
     */
    public void sendMessageFail1() throws InterruptedException {
        // 查找是否有发私信键
        List<WebElement> elements = driver.findElements(By.cssSelector("#index_div_message_list > div > div > div.col.text-truncate > a"));
        if (!elements.isEmpty()) {
            WebElement webElement = elements.get(0);
            // 确保私信键是可交互的的
            if (webElement.isDisplayed() && webElement.isEnabled()) {
                // 等待
                webElement.click();
                Thread.sleep(500);
                // 点击发送
                driver.findElement(By.cssSelector("#btn_index_message_reply")).click();
                // 捕获错误
                Thread.sleep(500);
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")));
                String text = driver.findElement(By.xpath("/html/body/div[4]/div")).getText();
                String[] texts = text.split("\n");
                assert texts[texts.length - 1].equals("请输入要发送的内容"); // 没有输入
            } else {
                System.out.println("不是发帖者");
            }
        } else {
            System.out.println("不是发帖者");
        }
    }


    /**
     * 私信发送失败, 点击取消
     */
    public void sendMessageFail() throws InterruptedException {
        // 进入帖子详情
        driver.get("http://127.0.0.1:58080/index.html");
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(3) > div > div.col > div.text-truncate > a > strong")).click();
        // 查找是否有发私信键
        List<WebElement> elements = driver.findElements(By.cssSelector("#btn_details_send_message"));
        if (!elements.isEmpty()) {
            WebElement webElement = elements.get(0);
            // 确保私信键是可交互的的
            if (webElement.isDisplayed() && webElement.isEnabled()) {
                webElement.click();
                // 文本框
                WebElement element = driver.findElement(By.cssSelector("#index_message_receive_content"));
                element.sendKeys("okok");
                Thread.sleep(200);
                driver.findElement(By.cssSelector("#index_message_receive_cancel")).click();
            } else {
                System.out.println("不是发帖者");
            }
        } else {
            System.out.println("不是发帖者");
        }
    }
}
